#include "config.h"

void ErasePage(u32 addr)
{
	__disable_irq();
		Flash_SectorErase(addr);
	__enable_irq();
}

u8t DFlash_ErasePage(u32 start, u32 end)
{
	u32 i, count;
	__disable_irq();
	for(i=start; i<end; i+=PAGE_SIZE)
		Flash_SectorErase(i);
	__enable_irq();
	count = end - start;
	for(i=0; i<count; i++)
	{
		if(*((volatile u8*)(start+i)) != 0xff)
            return 0;
	}
	return 1;
}

u8t DFlash_WritePage(u32 addr, u8 *buf, u16 count)
{
    u16 i;
    __disable_irq();
    Flash_SectorErase(addr);
    for(i=0; i<count; i++)
        Flash_WriteByte(addr+i, buf[i]);
    __enable_irq();
    for(i=0; i<count; i++)
    {
        if(*((volatile u8*)(addr+i)) != buf[i])
            return 0;
    }
    return 1;
}

u8t DFlash_WriteBytes(u32 start, u8 *buf, u16 count)
{
    u16 i;
    __disable_irq();
    for(i=0; i<count; i++)
        Flash_WriteByte(start+i, buf[i]);
    __enable_irq();
    for(i=0; i<count; i++)
    {
        if(*((volatile u8*)(start+i)) != buf[i])
            return 0;
    }
    return 1;
}

void DFlash_ReadBytes(u32 addr, u8 *buf, u16 count)
{
    u16 i;
    for(i=0; i<count; i++)
        buf[i] = *((volatile u8*)(addr+i));
}

u32t DFlash_CheckSum(u32 start, u32 end)
{
    u32 chksum = 0;
    u16 i,count;
    count = end - start;
    for(i=0; i<count; i++)
        chksum += *((volatile u8*)(start+i));
    return chksum;
}

void Print_FlashInfo(void)
{
	sPrint("Program Start...\r\n");
	sPrint("--\n");
	sPrint(strFILE);
	sPrint("\n--\n");
	sPrint(strDATE);
	sPrint("\n--\n");
	sPrint(strTIME);
}



